﻿using KPIS.GERP.Models.ICM;
using System;
using System.Linq;
using System.Web.Mvc;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class BidderCollateralController : Controller
    {
        public ActionResult Index(int BidderId)
        {
            var service = new BidService.BidServiceClient();
            var model = service.SelectBidderCollateral(new BidderCollateral { BidderId = BidderId }, new[] { "CollateralType", "CollateralStatus" });
            var modelBidder = service.SelectBidder(new Bidder { BidderId = BidderId }, new[] { "MasContact" }).FirstOrDefault();
            //var RecordStatus = service.SelectBidder(new Bidder { BidderId = BidderId }, new[] { "Bid" }).FirstOrDefault().Bid.RecordStatus;
            //var lstBidder = service.SelectBidder(new Bidder { BidderId = BidderId }, new[] { "Bid" , "PurchaseRequest"}).ToList();
            //var RecordStatus = "";
            //if (lstBidder.FirstOrDefault().PrId != null)
            //    RecordStatus = lstBidder.FirstOrDefault().PurchaseRequest.RecordStatus != "D" ? Models.SYS.RecordStatus.WaitStatus : lstBidder.FirstOrDefault().PurchaseRequest.RecordStatus;
            //else if (lstBidder.FirstOrDefault().BidId != null)
            //    RecordStatus = lstBidder.FirstOrDefault().Bid.RecordStatus;
            //else
            //    RecordStatus = Models.SYS.RecordStatus.WaitStatus;

            //ViewBag.bEditable = RecordStatus == Models.SYS.RecordStatus.WaitStatus || RecordStatus == Models.SYS.RecordStatus.ReturnedStatus;

            ViewBag.ModelBidder = modelBidder;

            return View(model);
        }

        public ActionResult Details(int Id)
        {
            var service = new BidService.BidServiceClient();
            var model = service.SelectBidderCollateral(new BidderCollateral { BidderColId = Id }, new[] { "Bidder.PurchaseRequest" ,"Bidder.Bid" ,"CollateralCategory", "CollateralType", "CollateralStatus" }).FirstOrDefault();
            var RecordStatus = "";

            if (model.Bidder.Bid != null)
                RecordStatus = model.Bidder.Bid.RecordStatus;
            else if (model.Bidder.PurchaseRequest != null)
                RecordStatus = model.Bidder.PurchaseRequest.RecordStatus != "D" ? Models.SYS.RecordStatus.WaitStatus : model.Bidder.PurchaseRequest.RecordStatus;
            else
                RecordStatus = Models.SYS.RecordStatus.WaitStatus;

            ViewBag.bEditable = RecordStatus == Models.SYS.RecordStatus.WaitStatus || RecordStatus == Models.SYS.RecordStatus.ReturnedStatus;

            return View(model);
        }

        public ActionResult Create(int BidderId)
        {
            var model = new Models.ICM.BidderCollateral();
            model.BidderId = BidderId;
            model.ColStatusId = 31;

            return View(model);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Create(BidderCollateral model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (string.IsNullOrWhiteSpace(model.Code))
                    {
                        ModelState.AddModelError("", "กรอกข้อมูลไม่ครบถ้วน!!");
                        return View(model);
                    }
                    var service = new BidService.BidServiceClient();
                    var id = service.InsertBidderCollateral(model, Class.GimsMembership.CurrentEmpId);

                    return RedirectToAction("Details", new { Id = id });
                }

                return View(model);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return View(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var service = new BidService.BidServiceClient();
            var model = service.SelectBidderCollateral(new BidderCollateral { BidderColId = Id }, null).FirstOrDefault();

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int Id, BidderCollateral model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (string.IsNullOrWhiteSpace(model.Code))
                    {
                        ModelState.AddModelError("", "กรอกข้อมูลไม่ครบถ้วน!!");
                        return View(model);
                    }
                    var service = new BidService.BidServiceClient();
                    service.UpdateBidderCollateral(model, Class.GimsMembership.CurrentEmpId);

                    return RedirectToAction("Details", new { Id = model.BidderColId });
                }

                return View(model);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Empty", ex.Message);
                return View(model);
            }
        }

        public ActionResult Delete(int Id)
        {
            try
            {
                var service = new BidService.BidServiceClient();
                var BidderId = service.SelectBidderCollateral(new BidderCollateral { BidderColId = Id }, null).FirstOrDefault().BidderId;
                service.DeleteBidderCollateral(Id, Class.GimsMembership.CurrentEmpId);

                return RedirectToAction("Index", new { BidderId = BidderId });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAll(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new BidService.BidServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.DeleteBidderCollateral(delete, Class.GimsMembership.CurrentEmpId);
                    }
                }

                ModelState.AddModelError("Empty", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");
                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Empty", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }
    }
}
